﻿using System.Collections.Generic;
using System.Web.Mvc;
using YBBEST.Office365Portal.Core;
using YBBEST.Office365Portal.MvcWebRole.ViewModels;

namespace YBBEST.Office365Portal.MvcWebRole.Controllers
{
    public class ListController : Controller
    {
        private readonly IListsRepository listsRepository;
        public ListController(IListsRepository listsRepository)
        {
            this.listsRepository = listsRepository;
        }
        // GET: /List/
        public ActionResult Index()
        {
            return View(listsRepository.RetrieveAllLists());
        }

        //
        // GET: /List/Details/5
        public ActionResult Details(string id)
        {
            string listTitle = listsRepository.RetrieveListByName(id).Title;
            List<string> listItems = listsRepository.DisplayListItems(listTitle);
            return View(new DocumentLibraryViewModel(listTitle,listItems));
        }

        public ActionResult Search(string listTitle)
        {
            return View(listsRepository.RetrieveListByName(listTitle));
        }

        //
        // GET: /List/Create

        public ActionResult Create()
        {
            var listTemplates= new List<ListTemplate>
                                                  {
                                                      new ListTemplate{TemplateId=100,Name = "Custom list"},
                                                      new ListTemplate{TemplateId=101,Name = "Document library"}   
                                                  };
            SelectList list = new SelectList(listTemplates, "TemplateId", "Name");
            ViewBag.ListTemplateIds = list;

            return View();
        } 

        //
        // POST: /List/Create

        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            try
            {
                // TODO: Add insert logic here
                string listName = form["Title"];
                string listDescription = form["Description"];
                int templateId;
                int.TryParse(form["TemplateId"], out templateId); 
                listsRepository.CreateNewList(listName,listDescription,templateId);

                return RedirectToAction("Index");
            }
            catch
            {
                //TODO:logging and return error view
                return View();

            }
        }
        
        //
        // GET: /List/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /List/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /List/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /List/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Checkout(string url, string file)
        {
            listsRepository.CheckOutFile(url+"/"+file);
            return RedirectToAction("Index");
        }
    }
}
